<template>
  <view class="container" :class="[device.lang]">
    <loginForm @handleLogIns="handleLogIn" />
    <bottom :type="SwiperType" />
  </view>
</template>

<script setup>
import loginForm from "./components/login-form.vue";
import { ref } from "vue";
import {
  HOME_PATH,
  LOGIN_PATH,
  isTabBarPath,
  removeQueryString,
} from "@/router";
import { useAuthStore } from "@/store";
import { onLoad } from "@dcloudio/uni-app";
import bottom from "./components/bottom.vue";
import { useDeviceStore } from "@/store/modules/device";
import { useI18n } from "vue-i18n";
import { setStorage } from "../../utils";
const { t } = useI18n();
const device = useDeviceStore();
console.log("🔥🔥🔥🔥🔥login", device.lang);

let redirect = HOME_PATH;
onLoad((options) => {
  if (options.redirect && removeQueryString(options.redirect) !== LOGIN_PATH) {
    redirect = decodeURIComponent(options.redirect);
  }
});
const SwiperType = ref("left");
const authStore = useAuthStore();

async function handleRegister() {
  await authStore.signUp({
    username: "admin",
    password: "admin123",
  });
  console.log(authStore.accessToken);
  uni.$uv.toast("注册成功");
  setTimeout(() => {
    uni.$uv.route({
      type: isTabBarPath(redirect) ? "switchTab" : "redirectTo",
      url: redirect,
    });
  }, 800);
}

async function handleLogIn(v) {
  await authStore.signIn({
    username: v.username,
    password: v.password,
  });
  setStorage("accessToken", authStore.accessToken);
  uni.$uv.toast("登录成功");
  setTimeout(() => {
    uni.navigateTo({
      url: "/pages/index/storeList",
    });
    // uni.$uv.route({
    // 	type: isTabBarPath(redirect) ? "switchTab" : "redirectTo",
    // 	url: redirect,
    // });
  }, 800);
}
</script>

<style lang="scss" scoped>
@import "@/static/css/index.scss";

.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background-color: $primary-a;
}

.Arab {
}
</style>
